/*     */ package com.chinapay.secss;
/*     */ 
/*     */ import java.security.Key;
/*     */ import java.security.Provider;
/*     */ import java.security.Security;
/*     */ import javax.crypto.Cipher;
/*     */ import javax.crypto.spec.SecretKeySpec;
/*     */ import org.bouncycastle.jce.provider.BouncyCastleProvider;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CPEncryptAndDecrypt
/*     */ {
/*  18 */   private String keyStr = "ChinaPay20150708";
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*  23 */   private String provider = "BC";
/*     */ 
/*     */   
/*     */   public static final String ENC_AES_ECB_PKCS5PADDING = "AES/ECB/PKCS5Padding";
/*     */ 
/*     */   
/*     */   static {
/*  30 */     Security.addProvider((Provider)new BouncyCastleProvider());
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public byte[] encryptPassword(byte[] orgBytes) throws Exception {
/*  39 */     byte[] encPassBytes = null;
/*  40 */     byte[] scrKeyBytes = this.keyStr.getBytes(SecssConstants.CHARSET_COMM);
/*  41 */     if (isEmpty(scrKeyBytes)) {
/*  42 */       throw new Exception("加密密钥为空，无法进行加密操作！");
/*     */     }
/*     */     try {
/*  45 */       encPassBytes = encryptWithSecretKey(orgBytes, scrKeyBytes, "AES/ECB/PKCS5Padding", 
/*  46 */           this.provider);
/*  47 */     } catch (Exception e) {
/*  48 */       LogUtil.writeErrorLog("加密所需密钥发错误！", e);
/*  49 */       throw new Exception("加密所需密钥发错误！");
/*     */     } 
/*  51 */     return encPassBytes;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public byte[] decryptPassword(byte[] orgBytes) throws Exception {
/*  60 */     byte[] decPassBytes = null;
/*  61 */     byte[] scrKeyBytes = this.keyStr.getBytes(SecssConstants.CHARSET_COMM);
/*  62 */     if (isEmpty(scrKeyBytes)) {
/*  63 */       throw new Exception("解密密钥为空，无法进行解密操作！");
/*     */     }
/*     */     try {
/*  66 */       decPassBytes = decryptWithSecretKey(orgBytes, scrKeyBytes, "AES/ECB/PKCS5Padding", 
/*  67 */           this.provider);
/*  68 */     } catch (Exception e) {
/*  69 */       LogUtil.writeErrorLog("解密密钥发错误！", e);
/*  70 */       throw new Exception("解密钥发错误！");
/*     */     } 
/*  72 */     return decPassBytes;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static byte[] decryptWithSecretKey(byte[] dataBytes, byte[] keyBytes, String algName, String provider) throws Exception {
/*  87 */     return doSecure(dataBytes, keyBytes, 2, algName, 
/*  88 */         provider);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static byte[] encryptWithSecretKey(byte[] dataBytes, byte[] keyBytes, String algName, String provider) throws Exception {
/* 102 */     return doSecure(dataBytes, keyBytes, 1, algName, 
/* 103 */         provider);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static byte[] doSecure(byte[] dataBytes, byte[] keyBytes, int mode, String algName, String provider) throws Exception {
/* 116 */     Key key = generateSecretKey(keyBytes, algName);
/* 117 */     return doSecure(dataBytes, key, mode, algName, provider);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static Key generateSecretKey(byte[] enCodeFormat, String algName) {
/* 126 */     String localName = algName;
/* 127 */     if (algName.contains("/")) {
/* 128 */       localName = localName.split("[/]")[0];
/*     */     }
/* 130 */     return new SecretKeySpec(enCodeFormat, localName);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static byte[] doSecure(byte[] dataBytes, Key key, int mode, String algName, String provider) throws Exception {
/* 143 */     Cipher cipher = Cipher.getInstance(algName, provider);
/* 144 */     cipher.init(mode, key);
/* 145 */     return cipher.doFinal(dataBytes);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static boolean isEmpty(byte[] bty) {
/* 153 */     return (bty == null);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String parseByte2HexStr(byte[] buf) {
/* 163 */     StringBuffer sb = new StringBuffer();
/* 164 */     for (int i = 0; i < buf.length; i++) {
/* 165 */       String hex = Integer.toHexString(buf[i] & 0xFF);
/* 166 */       if (hex.length() == 1) {
/* 167 */         hex = String.valueOf('0') + hex;
/*     */       }
/* 169 */       sb.append(hex.toUpperCase());
/*     */     } 
/* 171 */     return sb.toString();
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static byte[] parseHexStr2Byte(String hexStr) {
/* 179 */     if (hexStr.length() < 1)
/* 180 */       return null; 
/* 181 */     byte[] result = new byte[hexStr.length() / 2];
/* 182 */     for (int i = 0; i < hexStr.length() / 2; i++) {
/* 183 */       int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
/* 184 */       int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
/* 185 */       result[i] = (byte)(high * 16 + low);
/*     */     } 
/* 187 */     return result;
/*     */   }
/*     */ }


/* Location:              C:\Users\Administrator\Downloads\jd-gui-windows-1.6.6\jd-gui-windows-1.6.6\chinapay-secure-1.5.1.jar!\com\chinapay\secss\CPEncryptAndDecrypt.class
 * Java compiler version: 5 (49.0)
 * JD-Core Version:       1.1.3
 */